package com.hd.pzhwlxt.controller.model2;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.GoodsOrder;
import com.hd.pzhwlxt.pojo.GoodsPurchase;
import com.hd.pzhwlxt.service.GoodsOrderService;
import com.hd.pzhwlxt.service.GoodsPurchaseService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-26 9:04
 * @author: 怀念天空
 * @Description:
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsPurchaseService goodsPurchaseService;

    @Autowired
    private GoodsOrderService goodsOrderService;

    /**
     * 货物采购信息
     * @param
     * @return
     */
    @GetMapping("/goodsPurchase")
    public Response<Object> getAllPurchaseInfo(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<GoodsPurchase> result = goodsPurchaseService.getAllPurchaseInfo();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<GoodsPurchase> result = goodsPurchaseService.getAllPurchaseInfoPage(new Page<>(pageNum, pageSize));
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @DeleteMapping("/goodsPurchase/{ids}")
    public Response<String> deletePurchaseInfo(@PathVariable Integer[] ids){
        boolean remove = goodsPurchaseService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    /**
     * 采购模块，可以添加采购和查看采购记录
     * 注意：添加采购后，需手动确认采购状态，和支付状态。一旦采购成功不可修改，因为库存已经改变。
     */

    @PostMapping("/goodsPurchase")
    public Response<String> addPurchaseInfo(@RequestBody GoodsPurchase goodsPurchase){
        //添加时先计算金额
        boolean save = goodsPurchaseService.addPurchaseInfo(goodsPurchase);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @PutMapping("/goodsPurchase")
    public Response<String> updatePurchaseInfo(@RequestBody GoodsPurchase goodsPurchase){
        //修改时添加库存信息
        boolean update = goodsPurchaseService.updatePurchaseInfo(goodsPurchase);
        if (update){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    /*
    订单接口：
    添加订单、查询订单
     */
    @PostMapping("/goodsOrder")
    public Response<String> addGoodsOrder(@RequestBody GoodsOrder goodsOrder){
        boolean save = goodsOrderService.addGoodsOrder(goodsOrder);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    /**
     * 查询订单
     */
    @GetMapping("/goodsOrder")
    public Response<Object> getAllGoodsOrder(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<GoodsOrder> result = goodsOrderService.getAllGoodsOrder();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<GoodsOrder> result = goodsOrderService.getAllGoodsOrderPage(new Page<>(pageNum,pageSize));
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }
}
